package cn.yl.fxboot.application.action;

import java.io.File;
import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Autowired;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.yl.fxboot.application.common.config.GlobalConfig;
import cn.yl.fxboot.application.common.utils.SpringUtils;
import cn.yl.fxboot.application.common.utils.StringUtil;
import cn.yl.fxboot.application.controller.MainStageController;
import cn.yl.fxboot.application.service.DouyinHelper;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;

public class MixAction {

	public EventHandler<ActionEvent> startHandler(TextField mixAddr_TF,TextField mixSrc_TF) {
		return new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Task<Void> task = new Task<Void>() {
					@Override
					protected Void call() {
						try {
							Platform.runLater(()->{
								MsgPaneActions.doLog("创建任务中...");
							});
							String output = mixAddr_TF.getText();
							String input = mixSrc_TF.getText();
							TextField ef = (TextField) mixSrc_TF.getScene().lookup("#mixCount_TF");
							TextField headField = (TextField) mixSrc_TF.getScene().lookup("#mixVideoHead_TF");
							TextField endField = (TextField) mixSrc_TF.getScene().lookup("#mixVideoEnd_TF");
							String[] countStr = ef.getText().split(";");
							DouyinHelper.mixVideoByName(input, output,headField.getText(),endField.getText(), countStr);
							return null;
						}catch (Exception e) {
							Platform.runLater(()->{
								MsgPaneActions.doLog("error:"+e.getMessage());
							});
						}
						return null;
					}
				};
				Executor pool =  (Executor) SpringUtils.getBean("customServiceExecutor"); 
				pool.execute(task);
			}
			
		};
	}
	
	
	/**
	 * 选择源文件
	 * @param node
	 * @return
	 */
	public EventHandler<ActionEvent> mixSrcBtnHandler(TextField node) {
		return new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				DirectoryChooser DirectoryChooser = new DirectoryChooser();
				DirectoryChooser.setTitle("选择导入文件夹");
				File dir = DirectoryChooser.showDialog(node.getScene().getWindow());
				if(dir == null || !dir.isDirectory()) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setContentText("请选择文件夹");
					alert.setHeaderText("");
					alert.show();
				}else {
					node.setText(dir.getPath());
				}
			}
		};
	}
	
	public EventHandler<ActionEvent> mixAddrBtnHandler(TextField node) {
		return new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				DirectoryChooser dirChooser = new DirectoryChooser();
				dirChooser.setTitle("选择导出文件夹");
				// dirChooser.setInitialDirectory(new File("D:\\Documents"));
				File dir = dirChooser.showDialog(node.getScene().getWindow());
				if(dir == null || !dir.isDirectory()) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setContentText("请选择文件夹");
					alert.setHeaderText("");
					alert.show();
				}else {
					node.setText(dir.getPath());
				}
			}
		};
	}


	public EventHandler<ActionEvent> mixVideoHeadBtnHandler(TextField mixVideoHead_TF) {

		return new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				FileChooser fileChooser = new FileChooser();
				fileChooser.setTitle("选择导入文件");
				// dirChooser.setInitialDirectory(new File("D:\\Documents"));
				File file = fileChooser.showOpenDialog(mixVideoHead_TF.getScene().getWindow());
				//mixVideoHead_TF.setText(file.getAbsolutePath());
				if (FileUtil.isFile(file)) {
					String fileType = FileUtil.getType(file);
					if(!"mp4".equals(fileType) || !"ts".equals(fileType) ) {
						mixVideoHead_TF.setText(file.getAbsolutePath());
					}else {
						Alert alert = new Alert(AlertType.ERROR);
						alert.setContentText("请选择mp4或者ts格式的视频文件");
						alert.setHeaderText("");
						alert.show();
					}
				}else {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setContentText("请选择mp4或者ts格式的视频文件");
					alert.setHeaderText("");
					alert.show();
				}
			}
			
		};
	}


	public EventHandler<ActionEvent> mixVideoEndBtnHandler(TextField mixVideoEnd_TF) {
		return new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				FileChooser fileChooser = new FileChooser();
				fileChooser.setTitle("选择导入文件");
				// dirChooser.setInitialDirectory(new File("D:\\Documents"));
				File file = fileChooser.showOpenDialog(mixVideoEnd_TF.getScene().getWindow());
				if (FileUtil.isFile(file)) {
					String fileType = FileUtil.getType(file);
					if(!"mp4".equals(fileType) || !"ts".equals(fileType) ) {
						mixVideoEnd_TF.setText(file.getAbsolutePath());
					}else {
						Alert alert = new Alert(AlertType.ERROR);
						alert.setContentText("请选择mp4或者ts格式的视频文件");
						alert.setHeaderText("");
						alert.show();
					}
				}else {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setContentText("请选择mp4或者ts格式的视频文件");
					alert.setHeaderText("");
					alert.show();
				}
			}
		};
	}


	public EventHandler<ActionEvent> FFMPEGBtnHandler(TextField fFMPEG_TF) {
		return new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				FileChooser fileChooser = new FileChooser();
				fileChooser.setTitle("选择导入文件");
				// dirChooser.setInitialDirectory(new File("D:\\Documents"));
				File file = fileChooser.showOpenDialog(fFMPEG_TF.getScene().getWindow());
				fFMPEG_TF.setText(file.getAbsolutePath());
				GlobalConfig.FFMPEGPath = file.getAbsolutePath();
			}
			
		};
	}

}
